<template>
  <div class="ticket-view">
    <!-- 顶部导航栏 -->
    <van-nav-bar left-text="我的订单" left-arrow @click-left="goBack">
      <template #right>
        <van-goods-action-button icon="search" @click="goToSearch" />
        <van-goods-action-icon icon="cart-o" @click="goToShopcart" :badge="shopCartData.length" />
      </template>
    </van-nav-bar>
    <!-- 二级路由导航栏 -->
    <div class="top-nav">
      <van-tabs
        v-model="navActive"
        line-height="2px"
        color="rgb(252, 105, 3)"
        title-active-color="rgb(252, 105, 3)"
        title-inactive-color="#888"
        @click="changeRouter"
      >
        <van-tab
          v-for="item in topNav"
          :key="item.title"
          :title="item.title"
        ></van-tab>
      </van-tabs>
    </div>
    <keep-alive>
      <router-view @refesh="refesh"></router-view>
    </keep-alive>
  </div>
</template>

<script>
export default {
  name: "TicketView",
  data() {
    return {
      //导航栏
      topNav: [
        {
          title: "全部",
          name: "AllTicketView",
        },
        {
          title: "代付款",
          name: "PayingTicketView",
        },
        {
          title: "待收货",
          name: "ReceiveTicketView",
        },
        {
          title: "待评价",
          name: "CommentTicketView",
        },
      ],
      //导航栏下标
      navActive: 0,
      //购物车数据
      shopCartData:null,
    };
  },
  watch: {
    "$route.name"() {
      this.navActive = Number(window.localStorage.getItem("ticketViewIndex"));
    },
  },
  created() {
    //刷新时根据路由激活下标
    this.navActive = Number(window.localStorage.getItem("ticketViewIndex"));
    this.shopCartData=JSON.parse(localStorage.getItem('shopcartData'))
  },
  activated() {
    this.navActive = this.$route.params.index;
    localStorage.setItem("ticketViewIndex", JSON.stringify(this.navActive));
    this.topNav.forEach((item, index) => {
      if (index == this.navActive) {
        this.$router.push({ name: item.name });
      }
    });
    let commentTicket = JSON.parse(localStorage.getItem("commentTicket"));
    let payingData = JSON.parse(localStorage.getItem("payingData"));
    let payedData = JSON.parse(localStorage.getItem("payedData"));
    let commentedData = JSON.parse(localStorage.getItem("commentedData"));
    let arr = [];
    if (!commentTicket) {
      localStorage.setItem("commentTicket", JSON.stringify(arr));
    }
    if (!payingData) {
      localStorage.setItem("payingData", JSON.stringify(arr));
    }
    if (!payedData) {
      localStorage.setItem("payedData", JSON.stringify(arr));
    }
    if (!commentedData) {
      localStorage.setItem("commentedData", JSON.stringify(arr));
    }
  },
  methods: {
    //返回我的页面
    goBack() {
      this.$router.push({ name: "MyView" });
    },
    //跳转搜索页面
    goToSearch() {},
    //跳转购物车
    goToShopcart() {
      this.$router.push({ name: "ShopcartView" });
    },
    //二级路由切换
    changeRouter() {
      this.topNav.forEach((item, index) => {
        if (index == this.navActive) {
          if (this.$route.name != item.name) {
            this.$router.push({ name: item.name });
          }
        }
      });
      localStorage.setItem("ticketViewIndex", JSON.stringify(this.navActive));
    },
    refesh() {
      this.$router.go(0)
    },
  },
};
</script>

<style lang="scss" scoped>
.ticket-view {
  width: 100vw;
  height: 100vh;
  background-color: #f5f5f5;
  z-index: 30;
  position: fixed;
  top: 0;
  left: 0;
  overflow: auto;
  padding-top: 90px;
  ::v-deep .van-hairline--bottom {
    background-color: #f5f5f5;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
  }
  ::v-deep .van-button--default {
    background-color: #f5f5f5;
  }
  ::v-deep .van-goods-action-icon {
    background-color: #f5f5f5;
  }
  ::v-deep .van-icon {
    color: #888;
    font-size: 20px;
  }
  ::v-deep .van-nav-bar__arrow {
    color: #000;
  }
  ::v-deep .van-nav-bar__text {
    color: #000;
    font-size: 16px;
    margin-left: 10px;
  }
  ::v-deep .van-icon-cart-o {
    position: relative;
    top: 3px;
  }
  .top-nav {
    z-index: 10;
    position: fixed;
    top: 44px;
    left: 0;
    width: 100vw;
    padding-bottom: 1px;
  }
  ::v-deep .van-tabs__line {
    bottom: 16px;
  }
}
</style>